home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: Philippe Verdy <100105.3120@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Using the %c control in SCANF
- Date: 7 Apr 1996 23:26:58 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4k9j02$fl9@arl-news-svc-5.compuserve.com>
- NNTP-Posting-Host: ad04-110.compuserve.com
-
- 3mb42@qlink.queensu.ca (Ben David Moran) s'Θcrit :
- > Can anyone please give me some examples of using the %c control in the
- > scanf function.
- >
- > I keep getting segmentation faults when I use %c in the following line:
- >
- > scanf ("%s%c", textLine);
- >
- > the variable "textLine" is declared as
- >
- > char textLine[MAXBUFFERSIZE];
- >
- > while MAXBUFFERSIZE is an integer constant that equals 256.
- >
- > I am attempting to receive input, but not only up to the next non-blank
- > character. I want all of what is typed until the return key is hit.
- >
- > Can anyone help me use the %c control properly? If not, do you know of a
- > better way of accomplishing this, perhaps with iostream.h functions instead?
- >
- > Any help is appreciated, thanks.
- >
- > M
- >
- Your error is normal: with %s you read a line into textLine,
- then with %c you read a character which should be stored in
- the next variable. But you did not specify that variable as
- an argument.
- char textLine[MAXBUFFERSIZE], x;
- scanf("%s%c", textLine, x);
- Note that you must enter two lines prior to having results:
- %s reads all until the end-of line. then %c returns the first
- character of the next line entered.
-
- Using iostream you should do:
- char textLine[MAXBUFFERSIZE], x;
- cin >> textLine >> x;
-
- If your intent was to suppress the end of line from the
- returned string, do that after calling scanf().
-
- better solution: use: cin.getline(textLine, MAXBUFFERSIZE);
- from iostreams, which is type-safe.
-
-